//
//  QLPublishSetPriceVC.m
//  谁约
//
//  Created by qlwl on 16/10/20.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLPublishSetPriceVC.h"
#import "QLPublishSetPriceCell.h"
#import "AddressPicker.h"

@interface QLPublishSetPriceVC ()<PickerViewDataSource, PickerViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) QLPublishSetPriceModel * setPriceModel ; //
@property (nonatomic, strong) AddressPicker * timePicker ; //
@end

@implementation QLPublishSetPriceVC
{
    NSArray * _timeArray;
    __block int _selectRow;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self getMyNavBarWithNavTitle:@"价格设置" andRightTitle:@"保存" andRightIma:nil and:^(int tag) {
        if (self.setPriceModel.price == nil) {
            return ;
        }
//        NSString * achievePrice = [NSString stringWithFormat:@"%@%@",self.setPriceModel.price,self.setPriceModel.unitPrice];
        // 保存
        if (self.achievePrice) {
            self.achievePrice(self.setPriceModel);
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    _timeArray = @[@"10",@"20",@"30",@"40",@"50",@"60"];
}

- (QLPublishSetPriceModel *) setPriceModel
{
    if (!_setPriceModel) {
        _setPriceModel = [[QLPublishSetPriceModel alloc] init];
        _setPriceModel.unitType = UnitPriceType1;
        //_setPriceModel.time = @"不设置";
        _setPriceModel.payType = @"0";
    }
    return _setPriceModel;
}

#pragma mark TabDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        QLPublishSetPriceCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QLPublishSetPriceCell"];
        cell.priceField.text = self.setPriceModel.price;
        cell.unitPriceLab.text = self.setPriceModel.unitPrice;
        cell.inputPrice = ^(NSString * text){
            self.setPriceModel.price = text;
        };
        return cell;
    }else if (indexPath.row == 1){
        QLPublishChooseUnitPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QLPublishChooseUnitPriceCell"];
        cell.selectType = self.setPriceModel.unitType;
        cell.selectBtn = ^(ChooseUnitPriceType type){
            self.setPriceModel.unitType = type;
            [tableView reloadData];
        };
        return cell;
    }else if(indexPath.row == 2){
        QLPublishSetTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QLPublishSetTimeCell"];
        cell.timeLab.text = self.setPriceModel.time.length?self.setPriceModel.time:@"不设置";
        cell.setBtnClick = ^{
            if (!self.timePicker.isShow) {
                [self.view endEditing:YES];
                [self.timePicker showInView:self.view];
            }else{
                [self.timePicker remove];
            }
            
        };
        return cell;
    }else{
        QLChooseSKillPayTypeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QLChooseSKillPayTypeCell"];
        cell.type = self.setPriceModel.payType;
        cell.selectOffOrOnLine = ^(NSString * type){
            self.setPriceModel.payType = type;
        };
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].bounds.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark timePicker
- (AddressPicker *)timePicker
{
    if (!_timePicker) {
        _timePicker = [AddressPicker addressPickerView];
        _timePicker.delegate = self;
        _timePicker.dataSource = self;
        _timePicker.isLoding = NO;
        [_timePicker setTopHeadColor:NavBarTintColor];
        [_timePicker setTopTitle:@"服务时长设置"];
        [_timePicker setTopTitleColor:[UIColor whiteColor]];
        [_timePicker setSureOrCancelBtnColor:[UIColor whiteColor]];
        _timePicker.bgColor = [UIColor clearColor];
        typeof(self) weakSelf = self;
        _timePicker.sureBtnClick = ^{
            //获取时间
            //获取时间
            weakSelf.setPriceModel.unitType = UnitPriceType5;
            weakSelf.setPriceModel.time = _timeArray[_selectRow];
            [weakSelf.tableView reloadData];
        };
    }
    return _timePicker;
}


- (NSInteger)numberOfComponentsInPickerView:(AddressPicker *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(AddressPicker *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _timeArray.count;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_timeArray[row] stringByAppendingString:@"分钟"];
}
- (void)pickerView:(AddressPicker *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _selectRow = (int)row;
}

- (UIView *)pickerView:(AddressPicker *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:17]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}



@end
